package com.hunttown.mes.common.utils;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * created by wangjunfu on 2019-06-01
 */
public class ChineseToPinyinUtils {

    /**
     * 获取字符串拼音的第一个字母
     *
     * @param chinese 汉字
     * @return
     */
    public static String ToFirstChar(String chinese) {
        chinese = cleanChar(chinese);

        StringBuilder pinyinStr = new StringBuilder();
        char[] newChar = chinese.toCharArray();  //转为单个字符
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < newChar.length; i++) {
            if (newChar[i] > 128) {
                try {
                    pinyinStr.append(PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0].charAt(0));
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            } else {
                pinyinStr.append(newChar[i]);
            }
        }
        return pinyinStr.toString();
    }

    /**
     * 汉字转为拼音
     *
     * @param chinese 汉字
     * @return
     */
    public static String ToPinyin(String chinese) {
        chinese = cleanChar(chinese);

        StringBuilder pinyinStr = new StringBuilder();
        char[] newChar = chinese.toCharArray();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < newChar.length; i++) {
            if (newChar[i] > 128) {
                try {
                    pinyinStr.append(PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0]);
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            } else {
                pinyinStr.append(newChar[i]);
            }
        }
        return pinyinStr.toString();
    }

    /**
     * 汉字转为拼音 并且首字母大写
     *
     * @param chinese 汉字
     * @return
     */
    public static String ToPinyinFirstCharUpper(String chinese) {
        chinese = cleanChar(chinese);

        StringBuilder pinyinStr = new StringBuilder();
        char[] newChar = chinese.toCharArray();  //转为单个字符
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < newChar.length; i++) {
            if (newChar[i] > 128) {
                try {
                    String py = PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0];
                    py = py.substring(0, 1).toUpperCase() + py.substring(1, py.length());
                    pinyinStr.append(py);
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            } else {
                pinyinStr.append(newChar[i]);
            }
        }
        return pinyinStr.toString();
    }

    /**
     * 清理特殊字符以便得到
     *
     * @param chinese
     * @return
     */
    public static String cleanChar(String chinese) {
        // 正则去掉所有字符操作
        chinese = chinese.replaceAll("[\\p{Punct}\\p{Space}]+", "");

        // 正则表达式去掉所有中文的特殊符号
        String regEx = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}<>《》【】‘；：”“’。，、？]";
        Pattern pattern = Pattern.compile(regEx);
        Matcher matcher = pattern.matcher(chinese);
        chinese = matcher.replaceAll("").trim();
        return chinese;
    }

    /**
     * 测试main方法
     *
     * @param args
     */
    public static void main(String[] args) {
        //System.out.println(ToPinyinFirstCharUpper("枣园村"));
        //System.out.println(ToFirstChar("供应商").toUpperCase()); //转为首字母大写
        //System.out.println(ToPinyin("供应商"));
        //System.out.println(ToPinyinFirstCharUpper("供(应）商"));
        //System.out.println(ToPinyin("鲁KFW315"));

        System.out.println(ToFirstChar("供应商").toUpperCase()); //转为首字母大写
    }
}
